var app = getApp()
var api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {


    name: '',
    phone: '',
    password: '',
    shopname: '',
    address: '',
    tel: '',

  },



  //表单提交
  formSubmit: function(e) {
    wx.showModal({
      title: '提示',
      content: '是否确认保存信息',
      confirmColor: "#43CBC9",
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })
        if (res.confirm) {
          console.log('for发生了submit事件，携带数据为：', e.detail.value);
          if (e.detail.value.password.length == 0) {
            wx.showToast({
              title: "原始密码不能为空!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } else if (e.detail.value.newpassword.length == 0) {
            wx.showToast({
              title: "新密码不能为空!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } else if (e.detail.value.newpassword.length < 6) {
            wx.showToast({
              title: "新密码不能小于6个字符!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } else if (e.detail.value.confnewpassword.length == 0) {
            wx.showToast({
              title: "确认新密码不能为空!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } else if (e.detail.value.newpassword != e.detail.value.confnewpassword) {
            wx.showToast({
              title: "新密码输入不一致!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } else if (e.detail.value.newpassword == e.detail.value.password) {
            wx.showToast({
              title: "新密码不能与旧密码相同!",
              icon: 'none',
              duration: 2000
            });
            return false;
          }

          var userId = wx.getStorageSync('userId')
          userId = JSON.stringify(userId)

          wx.request({
            url: api.PasswordUpd,
            data: {
              password: e.detail.value.password,
              newPassword: e.detail.value.newpassword
            },
            method: 'GET',
            header: {
              'Accept': 'application/json',
              'ACCESS-TOKEN': wx.getStorageSync('token')
            },
            success: (res) => {
              wx.showLoading({
                title: '加载中',
              })
              if (res.data.success) {
                wx.hideLoading();
                wx.showModal({
                  title: '提示',
                  content: res.data.msg+'请您重新登录账号!',
                  confirmColor: "#43CBC9",
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      wx.clearStorageSync();
                      wx.reLaunch({
                        url: '/pages/login/loginnew/login',
                      })
                    }
                  }
                });
                
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 2000
                });
                return false;
              }
            }
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
          wx.hideLoading();
        }
      }
    })
  },

  formReset: function() {
    wx.showToast({
      title: "已取消操作!",
      icon: 'none',
      duration: 2000
    });
    wx.navigateBack({});
  },

 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})